package com.medical.seckill.controller;

import com.medical.common.utils.R;
import com.medical.seckill.service.SeckillService;
import com.medical.seckill.to.RedisGoodsTo;
import com.medical.seckill.to.SessionTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class SeckillController {
    @Autowired
    private SeckillService seckillService;

    /**
     * 获取当前所有能够秒杀的商品
     * @return
     */

    @RequestMapping("/seckillgoods")
    public R getCurrentGoodsAndSession(){
        List<RedisGoodsTo> goodsTos=seckillService.getCurrentGoodsAndSession();
        return R.ok().put("data",goodsTos);
    }
    /**
     * 获取两天内所有秒杀的场次
     */
    @RequestMapping("/session")
    public R getSession(){
        List<SessionTo> sessionVos=seckillService.getSession();
        return R.ok().put("data",sessionVos);
    }

    /**
     * 根据活动获取对应的商品
     */
    @RequestMapping("/goodsBySession")
    public R getGoodsBySession(@RequestParam("key") String key){
        List<RedisGoodsTo> goodsTos=seckillService.getGoodsBySession(key);
        return R.ok().put("data",goodsTos);
    }

    /**
     * 开始秒杀商品
     * @param kid
     * @param num
     * @param randomCode
     * @return
     */
    @RequestMapping("kill")
    public R startKill(@RequestParam("kid")String kid,
                       @RequestParam("num")Integer num,
                       @RequestParam("randomCode")String randomCode){
        String orderOn=seckillService.startKill(kid,num,randomCode);//返回订单号，即秒杀成功
        return R.ok().put("data",orderOn);
    }

}
